<?php

namespace app\admin\model;

use think\Model;
use traits\model\SoftDelete;

class Pageinfo extends Model
{
    use SoftDelete; //软删除功能，实际项目中对数据频繁使用删除操作会导致性能问题
    protected $deleteTime = 'delete_time';  //软删除字段,ThinkPHP5的软删除功能使用时间戳类型（数据表默认值为Null）
    protected $type = ['delete_time'  =>  'datetime'];  //写入和读取数据的时候都会自动处理成时间字符串Y-m-d H:i:s的格式。

    /**
     * @return array
     * @throws \think\exception\DbException
     */
    public function getLists()
    {
        $data = input('param.');    $limit = input('limit');
        $where['nid'] = $data['nid'];
        $data['title'] && $where['title_cn'] = ['like',"%{$data['title']}%"];

        $list = $this->field(['content_cn','content_en'],true)->where($where)->order('sort desc,add_time desc')->paginate($limit)->toArray();
        $list['code'] = 0;//用于前台数据表格判断成功字段
        return $list;
    }

    /**
     * 非列表式单页
     * @param $data
     * @param bool $isUpdate
     * @return array
     */
    public function _save1($data,$isUpdate = false)
    {
        $isUpdate && $where = ['nid'=>$data['nid']];
        if($this->validate(true)->allowField(true)->save($data,$where) !==false){
            return ajax_return(1,'保存成功');
        }else{
            return ajax_return(0,$this->getError());
        }
    }
    /**
     * 新增
     * @param integer $nid  栏目id
     * @return array
     */
    public function _save($nid = 0)
    {
        $data = input('post.'); //无需排除编辑器字段，获取时获取器自动转为html普通字符
        $nid && $data['nid'] = $nid;
        if(is_array($data['json_arr_cn']))$data['json_arr_cn'] = json_encode($data['json_arr_cn'],JSON_UNESCAPED_UNICODE);
        if(is_array($data['json_arr_en']))$data['json_arr_en'] = json_encode($data['json_arr_en'],JSON_UNESCAPED_UNICODE);
        $res = $this->validate(true)->allowField(true)->save($data);   //过滤非数据表字段数据
        if($res !== false){
            //单页类同一栏目下只能有一条是显示的
            if($data['is_show'] == 1)$this->where(['nid'=>$data['nid'],'id'=>['<>',$this->id]])->update(['is_show'=>0]);
            return ajax_return(1,'新增成功',session('redirect_url'));
        }else{
            return ajax_return(0,$this->getError());
        }
    }
    /**
     * 更新
     */
    public function _update()
    {
        $data = input('post.');
        if(is_array($data['json_arr_cn']))$data['json_arr_cn'] = json_encode($data['json_arr_cn'],JSON_UNESCAPED_UNICODE);
        if(is_array($data['json_arr_en']))$data['json_arr_en'] = json_encode($data['json_arr_en'],JSON_UNESCAPED_UNICODE);

        $res = $this->validate(true)->allowField(true)->isUpdate(true)->save($data);//isUpdate(true)显式指定更新数据操作
        if($res !== false){
            //单页类同一栏目下只能有一条是显示的
            if($data['is_show'] == 1)$this->where(['nid'=>$data['nid'],'id'=>['<>',$data['id']]])->update(['is_show'=>0]);
            return ajax_return(1,'更新成功',session('redirect_url'));   //更新成功后跳转编辑前页面
        }else{
            return ajax_return(0,$this->getError());
        }
    }
    /**
     * 软删除操作
     * @param mixed $id 待删除列表主键
     * @param bool $force   是否真实删除
     * @return array
     */
    public function _delete($id,$force = false)
    {
        if ($id) {
            $res = $this::destroy($id,$force);  //返回删除成功数（int）
            if($res > 0){
                return ajax_return(1,'删除成功');
            }else{
                return ajax_return(0,$this->getError());
            }
        }else {
            return ajax_return(0,'请选择需要删除的项目');
        }
    }
    /**
     * 获取器，读取数据时字段将html实体转为普通字符
     * @param $value
     * @return string
     */
    public function getContentCnAttr($value)
    {
        return htmlspecialchars_decode($value, ENT_QUOTES);
    }
    public function getContentEnAttr($value)
    {
        return htmlspecialchars_decode($value, ENT_QUOTES);
    }

}